<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户微信名" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入用户微信名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机号" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="zsxm">
        <el-input
          v-model="queryParams.zsxm"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="QQ号" prop="qq">
        <el-input
          v-model="queryParams.qq"
          placeholder="请输入QQ号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
  
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:oldUsers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:oldUsers:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:oldUsers:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="oldUsersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户微信名" align="center" prop="uname" />
      <el-table-column label="用户手机号" align="center" prop="tel" />
      <el-table-column label="用户信用值" align="center" prop="credit" />
      <el-table-column label="用户状态，禁用启用" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag :type="scope.row.type === 1 ? 'success' : 'danger'" size="mini">
            {{ scope.row.type === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="区分技术还是客户" align="center" prop="link">
        <template slot-scope="scope">
          <el-tag :type="scope.row.link === 0 ? 'primary' : 'success'" size="mini">
            {{ scope.row.link === 0 ? '技术' : '客户' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center" prop="zsxm" />
      <el-table-column label="QQ号" align="center" prop="qq" />
      <el-table-column label="用户支付宝" align="center" prop="zfb" />
      <el-table-column label="是否为55开技术" align="center" prop="sta">
        <template slot-scope="scope">
          <el-tag :type="scope.row.sta === 1 ? 'success' : 'info'" size="mini">
            {{ scope.row.sta === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="技术类型" align="center" prop="skillIds" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.skillIds">
            <el-tag 
              v-for="skillName in getSkillNames(scope.row.skillIds)" 
              :key="skillName" 
              size="mini" 
              style="margin-right: 5px;">
              {{ skillName }}
            </el-tag>
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="工作年限" align="center" prop="workYears">
        <template slot-scope="scope">
          <span>{{ scope.row.workYears ? scope.row.workYears + '年' : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:oldUsers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:oldUsers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
  
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改oldUsers对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户微信名" prop="uname">
              <el-input v-model="form.uname" placeholder="请输入用户微信名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户手机号" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入用户手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名" prop="zsxm">
              <el-input v-model="form.zsxm" placeholder="请输入真实姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="QQ号" prop="qq">
              <el-input v-model="form.qq" placeholder="请输入QQ号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户支付宝" prop="zfb">
              <el-input v-model="form.zfb" placeholder="请输入支付宝账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户信用值" prop="credit">
              <el-input-number v-model="form.credit" :min="0" :max="100" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户状态" prop="type">
              <el-select v-model="form.type" placeholder="请选择用户状态" style="width: 100%">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型" prop="link">
              <el-select v-model="form.link" placeholder="请选择用户类型" style="width: 100%">
                <el-option label="技术" :value="0"></el-option>
                <el-option label="客户" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作年限" prop="workYears">
              <el-input-number 
                v-model="form.workYears" 
                :min="0" 
                :max="50" 
                placeholder="请输入工作年限"
                style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="55开技术" prop="sta">
              <el-select v-model="form.sta" placeholder="请选择是否为55开技术" style="width: 100%">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="技术类型" prop="skillIds">
              <el-select
                v-model="selectedSkills"
                multiple
                placeholder="请选择技术类型"
                style="width: 100%"
                @change="handleSkillChange">
                <el-option
                  v-for="skill in skillOptions"
                  :key="skill.id"
                  :label="skill.name"
                  :value="skill.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  
  </div>
</template>

<script>
import { listOldUsers, getOldUsers, delOldUsers, addOldUsers, updateOldUsers } from "@/api/system/oldUsers.js"
import { listSkills } from "@/api/dashboard/skills.js"

export default {
  name: "OldUsers",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oldUsers表格数据
      oldUsersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uname: null,
        tel: null,
        credit: null,
        type: null,
        link: null,
        zsxm: null,
        qq: null,
        zfb: null,
        sta: null,
        skillIds: null,
        workYears: null,
        createBy: null
      },
      // 表单参数
      form: {},
      // 技能选项
      skillOptions: [],
      // 选中的技能
      selectedSkills: [],
      // 表单校验
      rules: {
        uname: [
          { required: true, message: "用户微信名不能为空", trigger: "blur" }
        ],
        tel: [
          { required: true, message: "用户手机号不能为空", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" }
        ],
        type: [
          { required: true, message: "用户状态不能为空", trigger: "change" }
        ],
        link: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList();
    this.getSkillList();
  },
  methods: {
    /** 查询oldUsers列表 */
    getList() {
      this.loading = true
      const params = this.addDateRange(this.queryParams, this.dateRange, 'CreateTime')
      listOldUsers(params).then(response => {
        this.oldUsersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uname: null,
        userpwd: null,
        tel: null,
        img: null,
        credit: null,
        type: null,
        link: null,
        openid: null,
        fxid: null,
        zsxm: null,
        qq: null,
        zfb: null,
        yqmoney: null,
        funddate: null,
        sta: null,
        skillIds: null,
        workYears: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加oldUsers"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOldUsers(id).then(response => {
        this.form = response.data;
        // 处理技能选择
        if (this.form.skillIds) {
          this.selectedSkills = this.form.skillIds.split(',');
        } else {
          this.selectedSkills = [];
        }
        this.open = true;
        this.title = "修改oldUsers";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOldUsers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOldUsers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除oldUsers编号为"' + ids + '"的数据项？').then(function() {
        return delOldUsers(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oldUsers/oldUsers/export', {
        ...this.queryParams
      }, `oldUsers_${new Date().getTime()}.xlsx`)
    },
    // 获取技能列表
    getSkillList() {
      listSkills().then(response => {
        this.skillOptions = response.rows || response.data || [];
      });
    },
    // 处理技能选择变化
    handleSkillChange(selectedIds) {
      this.form.skillIds = selectedIds.join(',');
    },
    // 获取技能名称
    getSkillNames(skillIds) {
      if (!skillIds) return [];
      const ids = skillIds.split(',');
      return ids.map(id => {
        const skill = this.skillOptions.find(s => s.id === id);
        return skill ? skill.name : id;
      }).filter(name => name);
    }
  }
}
</script>
